<template>
  <div class="period-detail-container">
    <!-- 时期信息展示区域 -->
    <div class="period-header" v-if="periodInfo">
      <div class="period-title">{{ periodInfo.periodName }}</div>
      <div class="period-year-tag">
        <span>{{ periodInfo.startYear }} - {{ periodInfo.endYear }}</span>
      </div>
      <div class="period-description">{{ periodInfo.description }}</div>
      <div class="period-decoration-line"></div>
    </div>

    <!-- 服装卡片网格 -->
    <div class="clothing-grid">
      <div
        v-for="item in clothingList"
        :key="item.id"
        class="clothing-card"
        @click="selectClothing(item)"
      >
        <div class="card-inner">
          <!-- 添加古风纹饰装饰角 -->
          <div class="corner-decoration top-left"></div>
          <div class="corner-decoration top-right"></div>
          <div class="corner-decoration bottom-left"></div>
          <div class="corner-decoration bottom-right"></div>

          <!-- 卡片主图 -->
          <div class="card-image-container">
            <div class="card-image">
              <img :src="item.imageUrl" :alt="item.clothingName" />
            </div>
            <!-- 添加红色印章装饰 -->
            <div class="card-seal" v-if="Math.random() > 0.5">
              <span>典藏</span>
            </div>
          </div>

          <!-- 卡片内容 -->
          <div class="card-content">
            <h3>{{ item.clothingName }}</h3>
            <div class="card-description">
              {{ item.description?.substring(0, 30) }}...
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 服装详情大卡 -->
    <div
      class="clothing-detail-card"
      v-if="selectedClothing"
      @click.self="closeDetail"
    >
      <div class="detail-card-container">
        <div class="detail-card-inner">
          <!-- 卡片顶部关闭按钮 -->
          <div class="close-btn" @click="closeDetail">×</div>

          <!-- 卡片标题区域 -->
          <div class="detail-card-header">
            <div class="detail-card-title">
              {{ selectedClothing.clothingName }}
            </div>
            <div class="detail-card-tabs">
              <div
                v-for="(tab, index) in tabs"
                :key="index"
                :class="['detail-tab', { active: activeTab === index }]"
                @click="activeTab = index"
              >
                {{ tab.label }}
              </div>
            </div>
          </div>

          <!-- 卡片内容区域 -->
          <div class="detail-card-content">
            <!-- 服饰简述 -->
            <div v-if="activeTab === 0" class="detail-tab-content">
              <div class="detail-image-container">
                <img
                  :src="selectedClothing.imageUrl"
                  :alt="selectedClothing.clothingName"
                />
                <div class="image-corner"></div>
              </div>
              <div class="detail-description">
                <p>{{ selectedClothing.description }}</p>
              </div>
            </div>

            <!-- 历史记忆 -->
            <div v-if="activeTab === 1" class="detail-tab-content">
              <h4>历史源流</h4>
              <p>{{ selectedClothing.historicalOrigin }}</p>
              <h4>区域分布</h4>
              <p>{{ selectedClothing.regionDistribution }}</p>
            </div>

            <!-- 材料工艺 -->
            <div v-if="activeTab === 2" class="detail-tab-content">
              <h4>穿着人群</h4>
              <p>{{ selectedClothing.wearingPopulation }}</p>
              <h4>工艺步骤</h4>
              <p v-html="formatCraftSteps(selectedClothing.craftSteps)"></p>
            </div>

            <!-- 纹样底蕴 -->
            <div v-if="activeTab === 3" class="detail-tab-content">
              <h4>纹样符号</h4>
              <p
                v-html="formatPatternSymbols(selectedClothing.patternSymbols)"
              ></p>
              <h4>象征意义</h4>
              <p>{{ selectedClothing.symbolicMeaning }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { message } from "ant-design-vue";
import {
  listDetailsUsingPost,
  listPeriodsUsingGet,
} from "@/api/clothingHistoryController";
import type API from "@/api";

const route = useRoute();
const clothingList = ref<API.ClothingHistoryDetail[]>([]);
const selectedClothing = ref<API.ClothingHistoryDetail | null>(null);
const activeTab = ref(0);
const periodInfo = ref<API.ClothingHistoryPeriod | null>(null);

// 定义标签页
const tabs = [
  { label: "服饰简述" },
  { label: "历史记忆" },
  { label: "材料工艺" },
  { label: "纹样底蕴" },
];

// 格式化工艺步骤，保留换行
const formatCraftSteps = (text: string | undefined) => {
  if (!text) return "";
  return text.replace(/\r\n/g, "<br>").replace(/\n/g, "<br>");
};

// 格式化纹样符号，保留换行
const formatPatternSymbols = (text: string | undefined) => {
  if (!text) return "";
  return text.replace(/\r\n/g, "<br>").replace(/\n/g, "<br>");
};

// 选择服装，显示详情
const selectClothing = (clothing: API.ClothingHistoryDetail) => {
  selectedClothing.value = clothing;
  activeTab.value = 0; // 重置为第一个标签页
};

// 关闭详情
const closeDetail = () => {
  selectedClothing.value = null;
};

// 加载时期服装列表
const loadClothingList = async () => {
  try {
    const periodId = Number(route.params.id);
    if (!periodId) return;

    const res = await listDetailsUsingPost({ id: periodId });
    if (res.data?.code === 0 && res.data.data) {
      clothingList.value = res.data.data;
    }
  } catch (error) {
    message.error("获取服装列表失败");
  }
};

// 加载时期信息
const loadPeriodInfo = async () => {
  try {
    const periodId = Number(route.params.id);
    if (!periodId) return;

    const res = await listPeriodsUsingGet();
    if (res.data?.code === 0 && res.data.data) {
      const foundPeriod = res.data.data.find(
        (period) => Number(period.id) === periodId
      );
      if (foundPeriod) {
        periodInfo.value = foundPeriod;
      } else {
        console.warn("未找到对应时期信息，periodId:", periodId);
        console.log("可用时期:", res.data.data);
      }
    }
  } catch (error) {
    console.error("获取时期信息失败", error);
  }
};

onMounted(async () => {
  await Promise.all([loadClothingList(), loadPeriodInfo()]);
});
</script>

<style scoped>
.period-detail-container {
  padding: 40px 24px;
  max-width: 1200px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
  background-color: #f9f5f0; /* 使用浅色背景替代背景图片 */
  min-height: 100vh;
}

/* 标题区域样式 */
.period-header {
  width: 90%;
  max-width: 800px;
  margin: 0 auto 40px;
  padding: 20px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  position: relative;
  text-align: center;
  border: 1px solid #e74c3c;
  overflow: hidden;
}

.period-header::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 6px;
  background-color: #e74c3c;
}

.period-title {
  font-size: 24px;
  font-weight: 700;
  color: #5c3c28;
  margin-bottom: 15px;
  position: relative;
  display: inline-block;
}

.period-title::after {
  content: "";
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 2px;
  background-color: #e74c3c;
}

.period-year-tag {
  display: inline-block;
  margin: 10px 0;
  background-color: rgba(231, 76, 60, 0.1);
  border: 1px solid #e74c3c;
  border-radius: 30px;
  padding: 5px 15px;
  color: #e74c3c;
  font-weight: 500;
}

.period-description {
  color: #666;
  line-height: 1.6;
  margin: 15px auto 0;
  max-width: 90%;
  text-align: justify;
  position: relative;
}

.period-decoration-line {
  width: 100%;
  height: 1px;
  background: linear-gradient(to right, transparent, #e74c3c, transparent);
  margin-top: 20px;
}

/* 卡片网格样式 */
.clothing-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 30px;
  width: 90%;
  margin: 0 auto;
}

.clothing-card {
  perspective: 1000px;
  cursor: pointer;
  max-width: 213px; /* 原来是320px, 现在是320 * 2/3 */
  margin: 0 auto;
}

.card-inner {
  position: relative;
  background-color: #f5ede3; /* 浅棕色背景 */
  border-radius: 8px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  padding: 8px;
  transition: all 0.4s ease;
  transform-style: preserve-3d;
  border: 1px solid #d5baaf;
}

/* 卡片装饰角 */
.corner-decoration {
  position: absolute;
  width: 15px; /* 原来是20px */
  height: 15px; /* 原来是20px */
  border: 2px solid #bc977b;
  z-index: 1;
}

.top-left {
  top: 4px;
  left: 4px;
  border-right: none;
  border-bottom: none;
}

.top-right {
  top: 4px;
  right: 4px;
  border-left: none;
  border-bottom: none;
}

.bottom-left {
  bottom: 4px;
  left: 4px;
  border-right: none;
  border-top: none;
}

.bottom-right {
  bottom: 4px;
  right: 4px;
  border-left: none;
  border-top: none;
}

/* 图片容器 */
.card-image-container {
  position: relative;
  padding: 4px;
  margin-bottom: 8px;
}

.card-image {
  position: relative;
  width: 100%;
  padding-top: 133.33%;
  overflow: hidden;
  border-radius: 6px;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
  background-color: #e9dfd0;
}

.card-image img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

/* 印章装饰 */
.card-seal {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 30px; /* 原来是40px */
  height: 30px; /* 原来是40px */
  background-color: #e74c3c;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: rotate(12deg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  z-index: 2;
}

.card-seal::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  right: 2px;
  bottom: 2px;
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 50%;
}

.card-seal span {
  color: #fff;
  font-size: 10px; /* 原来是12px */
  font-weight: bold;
}

/* 卡片内容样式 */
.card-content {
  padding: 8px 4px;
  text-align: center;
}

.card-content h3 {
  margin: 0 0 6px;
  color: #5c3c28;
  font-size: 16px; /* 原来是20px */
  font-weight: 700;
  letter-spacing: 1px;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.8);
  /* 艺术字体效果，使用描边 */
  -webkit-text-stroke: 0.3px #9c7a62;
}

.card-description {
  color: #7d5a44;
  font-size: 12px; /* 原来是14px */
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 卡片悬浮效果 */
.clothing-card:hover .card-inner {
  transform: translateY(-4px) rotateY(5deg);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.16);
}

.clothing-card:hover .card-image img {
  transform: scale(1.05);
}

/* 服装详情大卡样式 */
.clothing-detail-card {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  padding-top: 100px; /* 增加顶部留白，从60px改为100px */
  padding-bottom: 30px;
  box-sizing: border-box;
}

.detail-card-container {
  width: 65%;
  max-width: 560px;
  max-height: calc(100vh - 140px); /* 从100px调整为140px，确保留出足够空间 */
  background-color: #f8f3ea;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  position: relative;
  transition: all 0.3s ease;
  border: 1px solid #d8c4aa;
  height: auto;
  margin-top: 10px; /* 添加额外的上边距 */
}

.detail-card-inner {
  position: relative;
  padding: 20px;
  max-height: calc(100vh - 160px); /* 从120px调整为160px */
  height: auto;
  overflow-y: auto;
  border: 2px solid #e74c3c;
  margin: 6px;
  border-radius: 12px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
}

/* 添加滚动条样式 */
.detail-card-inner::-webkit-scrollbar,
.detail-tab-content::-webkit-scrollbar {
  width: 6px;
}

.detail-card-inner::-webkit-scrollbar-track,
.detail-tab-content::-webkit-scrollbar-track {
  background: #f0e0d6;
  border-radius: 10px;
}

.detail-card-inner::-webkit-scrollbar-thumb,
.detail-tab-content::-webkit-scrollbar-thumb {
  background-color: #e74c3c;
  border-radius: 10px;
}

.close-btn {
  position: absolute;
  top: 15px;
  right: 20px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: rgba(231, 76, 60, 0.8);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  cursor: pointer;
  z-index: 2;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background-color: rgba(231, 76, 60, 1);
  transform: scale(1.1);
}

.detail-card-header {
  margin-bottom: 20px;
  border-bottom: 1px solid #d8c4aa;
  position: relative;
}

.detail-card-header::after {
  content: "";
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 120px;
  height: 3px;
  background-color: #e74c3c;
}

.detail-card-title {
  font-size: 20px;
  font-weight: bold;
  color: #5c3c28;
  margin-bottom: 15px;
  text-align: center;
  letter-spacing: 2px;
  position: relative;
}

.detail-card-title::before,
.detail-card-title::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 30px;
  height: 1px;
  background-color: #e74c3c;
}

.detail-card-title::before {
  left: 20%;
}

.detail-card-title::after {
  right: 20%;
}

.detail-card-tabs {
  display: flex;
  margin-bottom: -1px;
  flex-wrap: wrap;
  justify-content: center;
}

.detail-tab {
  padding: 8px 12px;
  background-color: rgba(212, 196, 173, 0.3);
  color: #7d5a44;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  margin-right: 4px;
  margin-bottom: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s ease;
  border: 1px solid transparent;
  border-bottom: none;
}

.detail-tab.active {
  background-color: #f8f3ea;
  color: #e74c3c;
  font-weight: bold;
  border-color: #d8c4aa;
  border-bottom: 1px solid #f8f3ea;
  position: relative;
  z-index: 1;
}

.detail-tab:hover:not(.active) {
  background-color: rgba(212, 196, 173, 0.5);
}

.detail-card-content {
  padding: 15px 8px;
  line-height: 1.6;
  flex: 1;
  overflow: visible; /* 改为visible，让内容流动 */
}

.detail-tab-content {
  animation: fadeIn 0.3s ease;
  max-height: 350px; /* 限制每个标签页内容的最大高度 */
  overflow-y: auto; /* 添加垂直滚动 */
  padding-right: 10px; /* 为滚动条留出空间 */
}

.detail-image-container {
  width: 100%;
  text-align: center;
  margin-bottom: 20px;
  position: relative;
}

.detail-image-container img {
  max-width: 45%; /* 原来是60%，再缩小到当前的75%，约为45% */
  max-height: 190px; /* 原来是250px，再缩小到当前的75%，约为190px */
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  border: 1px solid #e74c3c;
  padding: 4px;
  background-color: white;
}

/* 调整图片角落装饰位置 */
.image-corner {
  position: absolute;
  bottom: 4px;
  right: calc(27.5% - 15px);
  width: 25px;
  height: 25px;
  background-color: #e74c3c;
  clip-path: polygon(100% 0, 0% 100%, 100% 100%);
}

.detail-description {
  color: #5c3c28;
  font-size: 16px;
  line-height: 1.8;
  text-align: justify;
  padding: 0 20px;
  border-left: 3px solid #e74c3c;
}

.detail-tab-content h4 {
  color: #e74c3c;
  font-size: 18px;
  margin: 20px 0 10px;
  border-left: 4px solid #e74c3c;
  padding-left: 10px;
  background-color: rgba(231, 76, 60, 0.05);
  padding: 5px 10px;
}

.detail-tab-content h4:first-child {
  margin-top: 0;
}

.detail-tab-content p {
  color: #5c3c28;
  font-size: 16px;
  line-height: 1.8;
  margin-bottom: 20px;
  text-align: justify;
  position: relative;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .clothing-grid {
    width: 95%;
    gap: 20px;
  }
}

@media screen and (max-width: 992px) {
  .clothing-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 25px;
  }

  .clothing-detail-card {
    padding-top: 90px;
  }

  .detail-card-container {
    width: 75%;
    max-height: calc(100vh - 120px);
  }
}

@media screen and (max-width: 768px) {
  .clothing-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }

  .card-content h3 {
    font-size: 14px;
  }

  .detail-card-tabs {
    flex-wrap: wrap;
  }

  .detail-tab {
    padding: 6px 10px;
    font-size: 12px;
  }

  .detail-card-title {
    font-size: 18px;
  }

  .period-header {
    padding: 15px;
    margin-bottom: 30px;
  }

  .period-title {
    font-size: 20px;
  }

  .period-description {
    font-size: 14px;
  }
}

@media screen and (max-width: 576px) {
  .clothing-grid {
    grid-template-columns: repeat(2, 1fr);
    width: 100%;
  }

  .clothing-card {
    max-width: 100%;
  }

  .clothing-detail-card {
    padding-top: 70px;
  }

  .detail-card-container {
    width: 90%;
    max-height: calc(100vh - 100px);
  }

  .detail-tab-content {
    max-height: 300px;
  }
}
</style>
